<?php

autoLoader();

$objDepartments	= new Departments();
$arrobjDepartments 	= $objDepartments->fetchAllDepartments();

$arrobjDeparmentTypes = new DepartmentTypes();
$arrobjDeparmentTypes = $arrobjDeparmentTypes->fetchAllDepartmentTypes();
$arrobjDeparmentTypes = rekeyObjects( 'Id', $arrobjDeparmentTypes );

if( false == valArr( $arrobjDepartments ) ) {
	echo '<h2>Departments are not avaliable.</h2>';
} else {
?>
<link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
<script src="js/plugins/dataTables/jquery.dataTables.js"></script>
<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>
<script src="js/bootstrap-confirmation.js" type="text/javascript"></script>
<script>
	$(document).ready(function() {
		$('#dataTables-example').dataTable();
	});
</script>
<script>
function DeleteData(id){
	var r = confirm( "Are You Sure Delete Department?" );
	if( r == true ) {
		$.ajax({
			type: "POST",
			url: "modules.php?file=delete_department",
			data:{did:id},
			beforeSend : function () {
			},
			success:function() {
				location.reload();
			}
		});
	}
}
</script>
<style>
	.thclass{
	font-size:12px;
	}
	.tbclass{
	font-size:12px;
	}
</style>
	<div style="margin-left:20px; margin-top: 10px;">
		<p>
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=department&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >Create New Department</a>&nbsp;
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=view_department&type=view&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >View All Departments</a>
		</p>
	</div>
<div class="col-lg-12">
	<div class="panel panel-primary">
		
		<div class="panel-heading"><i class="fa fa-bell fa-fw"></i>Department List</div>
		<div class="panel-body">
			<div class="table-responsive">
				<table class="table table-striped table-bordered table-hover" id="dataTables-example">
					<thead class="thclass">
						<tr>
							<th>Sr.No</th>
							<th>Name</th>
							<th>Depatment Type</th>
							<th>Dept Head Name</th>
							<th>Total Employee</th>
							<th>Contact Person</th>
							<th>Office No</th>
							<th>Edit</th>
							<th>Delete</th>
					</thead>
					<tbody class="tbclass">
					<?php 
						$intSnCount = 1;
						foreach( $arrobjDepartments as $objDepartment ) {
					?>
						<tr class="odd gradeX">
							<td><?php  echo $intSnCount; ?></td>
							<td><?php echo $objDepartment->getName(); ?></td>
							<td><?php $objDepartmentType = getArrayElementByKey( $objDepartment->getDepartmentTypeId(), $arrobjDeparmentTypes ); echo ( true == valObj( $objDepartmentType, 'BaseDepartmentTypes' ) ) ? $objDepartmentType->getName() : NULL; ?>
							<td><?php echo ( 'NULL' == $objDepartment->getDepartmentHeadName() ) ? '--' : $objDepartment->getDepartmentHeadName(); ?></td>
							<td><?php echo $objDepartment->getEmployeeCount(); ?></td>
							<td><?php echo $objDepartment->getContactPerson(); ?></td>
							<td><?php echo $objDepartment->getOfficeNumber(); ?></td>
							<td>
								<a href="modules.php?file=department&department_id=<? echo $objDepartment->getId(); ?>&mid=<? echo $mid;?>&smid=<? echo $smid;?>"><img src="images/icons/update.png" width="25" height="25"></a>
							</td>
							<td>
								<a id="deldept" onClick="DeleteData(<?echo $objDepartment->getId();?>);" ><img src="images/icons/delete.jpg" width="25" height="25"></a>
							</td>
						</tr>
					<?
					} 
					?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
<?php
}
?>